import asyncio
import aiohttp
import json
import os

async def debug_embedding_api():
    """详细调试硅基流动的embedding API"""
    
    url = "https://api.siliconflow.cn/v1/embeddings"
    api_key = "sk-xxjzwmnsxdodiqnfwtxkizjnplmexrpfomqzetjaeppvvrnl"
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "User-Agent": "LightRAG/1.3.9"
    }
    
    test_data = {
        "model": "BAAI/bge-m3",
        "input": "Silicon flow embedding online: fast, affordable, and high-quality embedding services."
    }
    
    print(f"请求URL: {url}")
    print(f"请求头: {headers}")
    print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
    
    async with aiohttp.ClientSession() as session:
        try:
            async with session.post(url, headers=headers, json=test_data) as response:
                print(f"\n响应状态码: {response.status}")
                print(f"响应头: {dict(response.headers)}")
                
                response_text = await response.text()
                print(f"响应内容: {response_text}")
                
                if response.status == 200:
                    result = json.loads(response_text)
                    if 'data' in result and len(result['data']) > 0:
                        embedding = result['data'][0]['embedding']
                        print(f"\n✅ 成功获取embedding!")
                        print(f"Embedding长度: {len(embedding)}")
                        print(f"Embedding维度: {len(embedding)}维")
                        print(f"Embedding前5个值: {embedding[:5]}")
                        print(f"Embedding后5个值: {embedding[-5:]}")
                    else:
                        print("❌ 响应中没有embedding数据")
                else:
                    print(f"❌ API请求失败，状态码: {response.status}")
                    
        except Exception as e:
            print(f"❌ 请求异常: {e}")

if __name__ == "__main__":
    asyncio.run(debug_embedding_api()) 